import intro.ex9.*;
/**
 * 
 * @author Daniel Shalev
 *
 *
*/

public abstract class SpaceShip{
	protected static final int TURNS_PER_SHOOT = 8;
	protected static final int REGAINED_ENERGY = 1;
	private static final int HIT_REDUCTION = 1;
	protected static final int DEFAULT_REG = 1;
	private static final int HEALTH_REDUCTION = 1;
	protected static final int INITIAL_ENERGY = 200;
	private static final int INITIAL_HEALTH = 10;
	protected static final int SHOT_ENERGY = 25;
	protected static final int SHEILD_ENERGY = 3;
    protected static final int TELEPORT_ENERGY = 150;
    protected static final int DONT_TURN = 0;
    protected static final int TURN_LEFT = 1;
    protected static final int TURN_RIGHT = -1;
	/**
    * The position and physics of the ship. 
    */
    protected SpaceShipPhysics pos;
    private int health;
    protected int energy;
    protected boolean shield;
    
    /**
	 * construct a new SpaceShip.
	 */
    public SpaceShip(){
    	this.pos=new SpaceShipPhysics();
    	this.health=INITIAL_HEALTH;
    	this.energy=INITIAL_ENERGY;
    	this.shield=false;
    }
    /**
    *  This method is called every time a collision with this ship occurs.
    */
    public void collidedWithAnotherShip() {
    	this.gotHit();
    }
    /**
    *  Does the actions of this ship for this round. 
    * This is called once per round by the SpaceWars game driver.
    * @param game the game object to which this ship belongs.
    */
    public abstract void doAction(SpaceWars game);
    /**
    *  Gets the image of this ship. This method should return the image of the
    *  ship with or without the shield. This will be displayed on the GUI at
    *  the end of the round.
    * @return the image of the ship.
    */
    public abstract java.awt.Image getImage();
    /**
    *  Gets the physics object that controls this ship.
    * @return the physics object that controls the ship.
    */
    public SpaceShipPhysics getPhysics() {
    	return this.pos;
    }
    /**
    *  This method is called by the SpaceWars game object when ever this ship
    *  gets hit by a shot.
    */
    public void gotHit() {
    	if (!this.shield)
    		this.health-=HIT_REDUCTION;
    }
    /**
    *  Checks if this ship is dead.
    * @return true if the ship is dead. false otherwise.
    */
    public boolean isDead() {
    	if(this.health<=0)
    		return true;
    	return false;
    }
    /**
    *  This method is called whenever a ship has died. It resets the ship's 
    *  attributes, and starts it at a new random position.
    */
    public void reset() {
    	this.pos=new SpaceShipPhysics();
    	this.health=INITIAL_HEALTH;
    	this.energy=INITIAL_ENERGY;
    	this.shield=false;
    }
    /**
     * teleport a space ship if it have enough energy and reduces energy neede for teleport
     */
    protected void teleport() {
        //checks if there is enough energy to teleport
        if(this.energy>=TELEPORT_ENERGY){
        	this.pos=new SpaceShipPhysics();
        	this.energy-=TELEPORT_ENERGY;
        }	
    }
    
    /**
     * the shooting method makes sure you only shoot once every 8 turns
     * makes the shoot and reduces needed energy gets the number of turns from last shot
     * and returns the updated turn
     * @param game the game object to which this ship belongs.
     * @param i the number of turns from last shot
     * @return updated number of turns
     */
    protected int shooting(SpaceWars game,int i) {
        //checks if enough time passed from last shot
        if (i<TURNS_PER_SHOOT)
        	return i+1;
        //checks if there is enough energy to shoot
    	if(this.energy>=SHOT_ENERGY){
            game.addShot(this.getPhysics());
            this.energy-=SHOT_ENERGY;
        }
    	return 0;
    }  
    
    /**
     * updates the energy of a space ship 
     * @param eToRegain
     */
    protected void setEnergy(int eToRegain){
    	//checks if energy is full
    	if(this.energy<INITIAL_ENERGY)
    		this.energy+=eToRegain;
    	//checks if energy level is less then 0 and sets it to 0 for Special class
    	if(this.energy<0)
    		this.energy=0;
    }
    
    /**
     * turns shield on if space ship has enough energy
     * in addition reduces needed energy
     */
    protected void turnShieldOn(){
    	//checks if this space ship have enough energy
    	if(this.energy>SHEILD_ENERGY){
    		//checks if shield is already on
    		if(!this.shield){
    			this.energy-=SHEILD_ENERGY;
    			this.shield=true;
    		}	
    	}	
    }
    
    /**
     * turns shield off
     */
    protected void turnShieldOff(){
    	this.shield=false;
    }
    
    /**
     * used for testing, returns the spaceship health
     * @return  the spaceship health
     */
    public int getHealth(){
        return this.health;
    }
    
    /**
     * used for testing, returns the spaceship energy
     * @return  the spaceship energy
     */
    public int getEnergy(){
        return this.energy;
    }
    
    /**
     * used for testing, returns the spaceship shield
     * @return  the spaceship shield
     */
    public boolean getShield(){
        return this.shield;
    }
    
    
}